package cnpm.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.JsonObject;

import cnpm.dto.UserDTO;
import cnpm.service.UserService;

/**
 * Servlet implementation class LoginController
 */
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/_login.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		boolean action = Boolean.parseBoolean(request.getParameter("action"));
		if(action){
			login(request, response);
		}else{
			logout(request, response);
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//boolean remember = Boolean.parseBoolean(request.getParameter("remember"));
		UserDTO user;
		ApplicationContext context = WebApplicationContextUtils
	            .getRequiredWebApplicationContext((getServletContext()));
		UserService userS = (UserService) context.getBean("userService");
		if(userS.isValidate(username, password)){
			HttpSession session = request.getSession();
			user = new UserDTO(username, userS.getUser(username).getFullname());
			session.setAttribute("User", user);
			response.getWriter().print(jsonLogin(true, "EmployeeController.do"));
		}else{
			response.getWriter().print(jsonLogin(false, "LoginController"));
		}
	}
	
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("User");
	}
	private JsonObject jsonLogin(boolean success, String location){
		JsonObject jsonResponse = new JsonObject();
		JsonObject jsonObj = new JsonObject();
		jsonObj.addProperty("success", success);
		jsonObj.addProperty("location", location);
		jsonResponse.add("Login", jsonObj);
		return jsonResponse;
	}
}
